﻿#include "passworddialog.h"
#include "ui_passworddialog.h"

PassWordDialog::PassWordDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PassWordDialog)
{
    ui->setupUi(this);
    setWindowFlags (Qt::Window | Qt::FramelessWindowHint);
    setWindowModality(Qt::ApplicationModal);
    setAttribute(Qt::WA_TranslucentBackground, true);//设置窗体背景透明

    initControl();
    m_inputStr.clear();
    m_ifShow = false;
    m_cleanFlag = 0;
    m_ipLength = 0;
    refreshUi();
}

PassWordDialog::~PassWordDialog()
{
    delete ui;
}

//初始化窗体控件，包括位置、尺寸、样式
void PassWordDialog::initControl()
{
    m_NumerBtnlist.clear();
    m_NumerBtnlist.append(ui->buttonNum7);
    m_NumerBtnlist.append(ui->buttonNum8);
    m_NumerBtnlist.append(ui->buttonNum9);
    m_NumerBtnlist.append(ui->buttonNum4);
    m_NumerBtnlist.append(ui->buttonNum5);
    m_NumerBtnlist.append(ui->buttonNum6);
    m_NumerBtnlist.append(ui->buttonNum1);
    m_NumerBtnlist.append(ui->buttonNum2);
    m_NumerBtnlist.append(ui->buttonNum3);
    m_NumerBtnlist.append(ui->buttonNum0);

    for(int var = 0; var < m_NumerBtnlist.size(); var++)
    {
        connect(m_NumerBtnlist.at(var),
                SIGNAL(clicked()),
                this,
                SLOT(numerBtnClick())
                );
    }

    //根据不同分辨率设置控件的位置和尺寸
    initResolution();
    initControlStyle();//初始化窗体控件样式
}

//初始化窗体控件为任意分辨率
void PassWordDialog::initResolution()
{
    #define FRAME_LEFT         (328)      // 小窗口
    #define FRAME_TOP          (80)
    #define FRAME_WIGHT        (367)
    #define FRAME_HEIGHT       (406)

    #define TITLE_HEIGHT       (48)       // 标题栏高度

    #define NUM_LEFT           (23)       // 数值左
    #define NUM_TOP            (85)       // 数值上
    #define NUM_WIGHT          (323)      // 数值宽度
    #define NUM_HEIGHT         (42)      // 数值高度

    #define NUM_CONT_X         (FRAME_LEFT+NUM_LEFT)     // 中心点x
    #define NUM_CONT_Y         (FRAME_TOP+NUM_TOP+NUM_HEIGHT)      // 中心点y

    double factoryX = getFactoryX();
    double factoryY = getFactoryY();

    this->resize(GLB_SCR_WIGHT*factoryX,GLB_SCR_HEIGHT*factoryY);
    this->move((GLB_SCR_WIGHT*factoryX-this->width())/2+g_mainWidgetPos.x(),(GLB_SCR_HEIGHT*factoryY-this->height())/2+g_mainWidgetPos.y());
    ui->frameBack->setGeometry(0*factoryX,0*factoryY,GLB_SCR_WIGHT*factoryX,GLB_SCR_HEIGHT*factoryY);
    ui->labelTitle->setGeometry((FRAME_LEFT+GLB_EDGE_WIGHT)*factoryX,(FRAME_TOP+(TITLE_HEIGHT-GLB_TEXT_HEIGHT)/2)*factoryY,(FRAME_WIGHT-GLB_TAB_WIGHT*2)*factoryX,GLB_TEXT_HEIGHT*factoryY);
    ui->labelValue->setGeometry((FRAME_LEFT+NUM_LEFT+GLB_TAB_WIGHT)*factoryX,(FRAME_TOP+NUM_TOP)*factoryY,(NUM_WIGHT-GLB_TAB_WIGHT*2)*factoryX,GLB_TEXT_L_HEIGHT*factoryY);

    int bgX,bgY;
    bgX = NUM_CONT_X;
    bgY = NUM_CONT_Y + GLB_EDGE_WIGHT;

    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 3; j ++)
        {
            if(i*3+j == m_NumerBtnlist.size()-1)
            {
                m_NumerBtnlist[i*3+j]->setGeometry((bgX+(j+1)*(GLB_LBUT_WIGHT+GLB_BAS_WIGHT))*factoryX,(bgY+i*(GLB_TEXT_L_HEIGHT+GLB_TAB_WIGHT))*factoryY,
                                                   GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
                break;
            }
            else
            {
                m_NumerBtnlist[i*3+j]->setGeometry((bgX+j*(GLB_LBUT_WIGHT+GLB_BAS_WIGHT))*factoryX,(bgY+i*(GLB_TEXT_L_HEIGHT+GLB_TAB_WIGHT))*factoryY,
                                                   GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
            }
        }
    }

    ui->buttonClean->setGeometry((bgX)*factoryX, (bgY+3*(GLB_TEXT_L_HEIGHT+GLB_TAB_WIGHT))*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonBack->setGeometry((bgX+2*(GLB_LBUT_WIGHT+GLB_BAS_WIGHT))*factoryX,(bgY+3*(GLB_TEXT_L_HEIGHT+GLB_TAB_WIGHT))*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonOk->setGeometry((bgX+1*(GLB_LBUT_WIGHT+GLB_BAS_WIGHT))*factoryX,(bgY+4*(GLB_TEXT_L_HEIGHT+GLB_TAB_WIGHT)+GLB_BAS_WIGHT)*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);
    ui->buttonCancel->setGeometry((bgX+2*(GLB_LBUT_WIGHT+GLB_BAS_WIGHT))*factoryX,(bgY+4*(GLB_TEXT_L_HEIGHT+GLB_TAB_WIGHT)+GLB_BAS_WIGHT)*factoryY,GLB_LBUT_WIGHT*factoryX,GLB_LBUT_HEIGHT*factoryY);


}

//初始化窗体控件样式
void PassWordDialog::initControlStyle()
{
    SetStyle setControlStyle;
    setControlStyle.setUiName(this->objectName());

    for(int i = 0; i < m_NumerBtnlist.size(); i++)
    {
        m_NumerBtnlist[i]->setStyleSheet(numButtonStyle());
        m_NumerBtnlist[i]->setFont(fontSize_M());
    }

    ui->frameBack->setStyleSheet(setControlStyle.getStyleSheet(this->objectName()));

    ui->labelTitle->setStyleSheet(titleTextColour());
    ui->labelTitle->setFont(fontSize_M());
    ui->labelTitle->setAlignment(Qt::AlignLeft);
    ui->labelTitle->setAlignment(Qt::AlignVCenter);

    ui->labelValue->setStyleSheet(warnTextColour());
    ui->labelValue->setFont(fontSize_L());
    ui->labelValue->setAlignment(Qt::AlignCenter);

    ui->buttonClean->setStyleSheet(numButtonStyle());
    ui->buttonClean->setFont(fontSize_M());
    ui->buttonBack->setStyleSheet(numButtonStyle());
    ui->buttonBack->setFont(fontSize_M());

    ui->buttonOk->setStyleSheet(confirmIconStyle());
    ui->buttonOk->setTopImage(setControlStyle.getSharedTopStyleSheet("buttonOk"),12);
    ui->buttonCancel->setStyleSheet(confirmIconStyle());
    ui->buttonCancel->setTopImage(setControlStyle.getSharedTopStyleSheet("buttonCancel"),12);

}

void PassWordDialog::refreshUi()
{
    QTextCodec *cod = QTextCodec::codecForLocale();
    QString showStr("");
    QString password = cod->toUnicode("●");
    for (int var = 0; var < m_inputStr.size(); var++)
    {
        showStr = showStr + password;
    }

    if(m_ifShow == false)//不可见
    {
        ui->labelValue->setText(showStr);
    }
    else//可见
    {
      ui->labelValue->setText(m_inputStr);
    }


    if(m_inputStr.length() > 0)
    {
        ui->buttonOk->setEnabled(true);
    }
    else
    {
        ui->buttonOk->setEnabled(false);
    }
}

void PassWordDialog::numerBtnClick()
{
    QPushButton *button = (QPushButton*) this->sender() ;

    if(button == NULL)
    {
        return;
    }
    QString str = button->text();
    m_inputStr.append(str);
    refreshUi();
}

void PassWordDialog::setTitleStr(QString str,int cFlag)
{
    ui->labelTitle->setText(str);
    m_inputStr.clear();
    if(cFlag != 0)
    {
        m_cleanFlag = cFlag;
        ui->buttonClean->setText(".");
    }
    refreshUi();
}

void PassWordDialog::setShowIfVisible(bool bShow)
{
    m_ifShow = bShow;
}

void PassWordDialog::setIpStr(QString ip)
{
    m_inputStr= ip;
    m_ipLength = ip.length();
    refreshUi();
}

QString PassWordDialog::getInputStr()
{
    return m_inputStr;
}
void PassWordDialog::on_buttonClean_clicked()
{
    if(m_cleanFlag != 0)
    {
        QString str = ui->buttonClean->text();
        m_inputStr.append(str);
    }
    else
    {
        m_inputStr.clear();
    }
    refreshUi();
}

void PassWordDialog::on_buttonBack_clicked()
{
    if(m_inputStr.length() <= m_ipLength)
    {
        return;
    }
    m_inputStr = m_inputStr.mid(0 , m_inputStr.size() - 1);
    refreshUi();
}

void PassWordDialog::on_buttonOk_clicked()
{
    if(m_inputStr.indexOf(".") != -1)
    {
        //检测IP地址是否合法
        QRegExp regExp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
        if (!regExp.exactMatch(m_inputStr))
        {
            PromptDialog promptDlg(this);
            promptDlg.initDialog(PromptDialog::BTN_OK);
            promptDlg.setTitleStr(tr("Prompt"));
            QString str = tr("IP settings are unreasonable!");//IP设置不合理!
            promptDlg.setContentStr(str);
            promptDlg.exec();

            return;
        }
    }

    done(1);
}

void PassWordDialog::on_buttonCancel_clicked()
{
    m_inputStr.clear();
    done(0);
}
